package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/wiggle-subsequence/">376. 摆动序列</a>
 */
public class Solution376 extends BaseSolution {

    public int wiggleMaxLength(int[] nums) {
        if (nums.length <= 1) {
            return 1;
        }
        int offset = nums[1] - nums[0];
        int last = nums[1];
        int p = 2;
        int rst = offset == 0 ? 1 : 2;
        while (p < nums.length) {
            int nowOffset = nums[p] - last;
            if (offset * nowOffset < 0 || (offset == 0 && nowOffset != 0)) {
                rst++;
                offset = nowOffset;
            } else {
                offset += nowOffset;
            }
            last = nums[p];
            p++;
        }
        return rst;
    }

    public static void main(String[] args) {
        Solution376 solution = new Solution376();
        System.out.println((solution.wiggleMaxLength(new int[] {
                0, 0, 1
        })));
    }
}